#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include <fcntl.h>
#include <sys/wait.h>


/*
 * 从标准输入读命令并执行
 */


int main(void)
{
	char buf[1024];
	pid_t pid;
	int status;

	printf("%% ");	/* print pormpt (printf requires %% to print %) */
	while (fgets(buf, 1024, stdin) != NULL) {
		if (buf[strlen(buf) - 1] == '\n')
			buf[strlen(buf) - 1] = 0;	/* replace newline with null */

		if (( pid = fork()) < 0 )
			perror("fork error");
		else if (pid == 0) {	/* child */
			execlp(buf, buf, (char *) 0);
			printf("couldn't execute : %s", buf);
			exit(127);
		}

		/* parent */
		if ((pid = waitpid(pid, &status, 0)) < 0)
			perror("waitpid error");
		printf("%% ");
	}

	exit(0);
}



